package com.yzyz.web.controller.visitor.news;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.yzyz.bean.news.NewsBean;
import com.yzyz.bean.scenic.PictureBean;
import com.yzyz.data.CacheFactory;

/**
 * 向客户端输出新闻的图片,图片规格是：400 x 300
 * @author David Sun
 * @Date   2010-4-23
 *
 */
public class NewsPictureController extends AbstractController {
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		NewsBean news = CacheFactory.getNewsStore().get(Integer.valueOf(request.getParameter("newsId")));
		PictureBean picture = news.getPictures().get(0);
		response.setContentType("image/jpeg");
		response.setHeader("Content-Disposition", "attachment; filename=" + news.getName());//设置图片的名字
		response.setContentLength(picture.getContents().length);
		OutputStream out = response.getOutputStream();
		out.write(picture.getContents());
		out.flush();
		out.close();
		return null;
	}

}
